Skip to content

feat(cohorts): Amplitude cohort sync endpoints and sync keys - #8290

Merged
gagantrivedi merged 15 commits into
mainfrom
feat/cohort-sync-amplitude
Aug 25, 2026
Merged

feat(cohorts): Amplitude cohort sync endpoints and sync keys#8290
gagantrivedi merged 15 commits into
mainfrom
feat/cohort-sync-amplitude

Conversation

@gagantrivedi

@gagantrivedi gagantrivedi commented Aug 14, 2026

Copy link
Copy Markdown
Member
  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Contributes to Flagsmith/flagsmith-private#260.

The receiving side of Amplitude's list-based cohort sync contract, feeding the existing membership ledger. Spec copied from Create a cohort sync integration and Receiving behavioral cohorts — routes, field names and status codes are fixed by Amplitude.

  • CohortSyncKey: per-environment bearer credential, hashed at rest, plaintext returned once; managed at /environments/<key>/cohorts/sync-keys/.
  • /cohort-sync/amplitude/lists/ creates the cohort and returns list_id; lists/<uuid>/add|remove/ turn {"user_ids": [...]} into pending ledger rows for the existing apply task.
  • Cohorts created this way write their own audit log, which is also what rebuilds the environment document.

How did you test this code?

Unit tests cover the auth matrix, list creation, add/remove reaching (moto) DynamoDB via the apply task, the 404 fences, and key management.

Beyond that, this can only really be tested once Amplitude grants us Integration Portal access: the endpoints are exercised for real by Amplitude's own calls, first through the portal's testing tab and Release internally mode, then in production.

@vercel

vercel Bot commented Aug 14, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Aug 25, 2026 6:08am
flagsmith-frontend-preview Ignored Ignored Preview Aug 25, 2026 6:08am
flagsmith-frontend-staging Ignored Ignored Preview Aug 25, 2026 6:08am

Request Review

@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 0b5fe3df-e89f-404d-8f3e-edbf412d7d55

📥 Commits

Reviewing files that changed from the base of the PR and between 3964c41 and 040cac7.

📒 Files selected for processing (2)
  • mcp/src/flagsmith_mcp/openapi.json
  • openapi.yaml

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Adds Bearer-token authentication and environment-scoped lifecycle management for cohort sync keys. Adds Amplitude cohort creation and member add/remove endpoints. Persists source types, records audit events, updates membership state, and documents the API and security scheme. Adds unit tests for authentication, validation, environment isolation, membership updates, audit records, and key revocation.

Estimated code review effort: 4 (Complex) | ~60 minutes

Merge Risk: 🟠 High · up to 040ca

The new Amplitude cohort-sync endpoints can acknowledge requests that later fail to apply membership changes, especially for oversized identifiers or removals, unavailable synchronization services, transaction timing failures, or audit-write failures; the API schema also does not fully match runtime requirements. These are high-impact correctness and data-consistency risks, so the PR is not ready to merge without fixes or explicit acceptance.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added api Issue related to the REST API docs Documentation updates feature New feature or request and removed docs Documentation updates labels Aug 14, 2026
@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.79%. Comparing base (65ce12c) to head (78ab6b7).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8290      +/-   ##
==========================================
+ Coverage   98.64%   98.79%   +0.14%     
==========================================
  Files        1609     1614       +5     
  Lines       64570    64988     +418     
==========================================
+ Hits        63696    64204     +508     
+ Misses        874      784      -90     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added feature New feature or request docs Documentation updates and removed feature New feature or request docs Documentation updates labels Aug 17, 2026
@github-actions github-actions Bot added feature New feature or request docs Documentation updates and removed feature New feature or request docs Documentation updates labels Aug 17, 2026
@gagantrivedi
gagantrivedi marked this pull request as ready for review August 17, 2026 09:29
@gagantrivedi
gagantrivedi requested review from a team as code owners August 17, 2026 09:29
@gagantrivedi
gagantrivedi removed the request for review from a team August 17, 2026 09:29
@github-actions github-actions Bot added the docs Documentation updates label Aug 19, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

♻️ Duplicate comments (1)
openapi.yaml (1)

1661-1737: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Bearer authentication is still undocumented for the Amplitude cohort-sync operations.

The three operations api_v1_cohort_sync_amplitude_lists_create, api_v1_cohort_sync_amplitude_lists_add_create, and api_v1_cohort_sync_amplitude_lists_remove_create have no security field. CohortSyncKeyAuthentication requires Authorization: Bearer <sync-key> for these endpoints, per the PR objectives.

A prior review thread on this file flagged the identical issue and recorded it as resolved by a commit that added a "Cohort Sync Key" security-scheme extension in api/api/openapi.py, with the note that CI regenerates this root openapi.yaml automatically from that source. In this file version, the fix has not taken effect:

  • Lines 1661-1737 still show no security field on the three operations.
  • The securitySchemes block (around line 29354) still lists only Environment API Key, Master API Key, basicAuth, and tokenAuth. No "Cohort Sync Key" scheme is defined.

Confirm that the authentication extension still exists and is wired to these operations, and that the generation step that produces this file actually runs before merge.

#!/bin/bash
# Description: Verify the Cohort Sync Key OpenAPI authentication extension exists and is applied.
set -euo pipefail

echo '--- Check for the authentication extension in api/api/openapi.py ---'
fd -a openapi.py api/api | xargs -r rg -n -C 5 'CohortSyncKeyAuthentication|OpenApiAuthenticationExtension|Cohort Sync Key'

echo '--- Check CohortSyncKeyAuthentication usage in cohorts sync views ---'
fd -a sync_views.py api/cohorts | xargs -r rg -n -C 3 'authentication_classes|CohortSyncKeyAuthentication'

echo '--- Check sdk/openapi.yaml for the scheme ---'
rg -n -C 3 'Cohort Sync Key' sdk/openapi.yaml || echo 'not found in sdk/openapi.yaml'

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 94df6b2d-a068-4e8b-a583-933430a093e8

📥 Commits

Reviewing files that changed from the base of the PR and between b439ee4 and 3964c41.

📒 Files selected for processing (7)
  • api/cohorts/services.py
  • api/cohorts/sync_views.py
  • api/cohorts/views.py
  • api/tests/unit/cohorts/test_sync_views.py
  • api/tests/unit/cohorts/test_views.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md
  • openapi.yaml

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/cohorts/services.py
Comment thread api/cohorts/services.py
Comment thread api/cohorts/sync_views.py
Comment thread docs/docs/deployment-self-hosting/observability/_events-catalogue.md Outdated
@gagantrivedi
gagantrivedi requested review from Zaimwa9 and removed request for khvn26 August 24, 2026 04:24
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request docs Documentation updates labels Aug 24, 2026
@github-actions github-actions Bot added docs Documentation updates feature New feature or request and removed feature New feature or request docs Documentation updates labels Aug 24, 2026
@github-actions github-actions Bot added the docs Documentation updates label Aug 24, 2026

@Zaimwa9 Zaimwa9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great! Just wondering whether we could gate on the org plan and have the cohort creation and audit in the same transaction to avoid zombie cohort and revert entirely in case of failure

Comment thread api/cohorts/services.py
Comment thread api/cohorts/sync_views.py
Comment thread api/cohorts/sync_views.py
@github-actions github-actions Bot added feature New feature or request and removed feature New feature or request docs Documentation updates labels Aug 25, 2026
@github-actions github-actions Bot added the docs Documentation updates label Aug 25, 2026

@Zaimwa9 Zaimwa9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@gagantrivedi
gagantrivedi removed the request for review from a team August 25, 2026 09:48
@gagantrivedi
gagantrivedi merged commit cc20443 into main Aug 25, 2026
42 checks passed
@gagantrivedi
gagantrivedi deleted the feat/cohort-sync-amplitude branch August 25, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API docs Documentation updates feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants